package com.fz.us.oa.web.shiro.utils;

import com.fz.us.base.util.LogUtil;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * 工具类 - Struts
 * ============================================================================

 * ============================================================================
 */

public class StrutsUtil {
	/**
	 * Cookieの追加
	 * @return
	 * @throws Exception
	 */
	public static void addCookie(HttpServletResponse response,String name,String value){
		Cookie cookie = new Cookie(name, value);
		cookie.setMaxAge(60*60*24*365);
		cookie.setPath("/");
		response.addCookie(cookie);
	}
	/**
	 * Cookieの、删除
	 * @return
	 * @throws Exception
	 */
	public static void deleteCookie(HttpServletRequest request,HttpServletResponse response,String name){
		Cookie[] cookies = request.getCookies();
		if(cookies != null){
			for(Cookie cookie : cookies)
			{
				LogUtil.info("size:" + cookies.length + "cookie{getName()}:" + cookie.getName() + " {value}:" + cookie.getValue());
				if(cookie.getName().equals(name))
				{
					cookie.setMaxAge(0);
					cookie.setValue("");
					cookie.setPath("/");
					response.addCookie(cookie);
					//return cookie.getValue();
				}
			}
		}
	}
	/**
	 * Cookieの取得
	 * @return
	 * @throws Exception
	 */
	public static String getCookie(HttpServletRequest request,String name){
		Cookie[] cookies = request.getCookies();
		if(cookies != null){
			for(Cookie cookie : cookies)
			{
				if(cookie.getName().equals(name))
				{
					return cookie.getValue();
				}
			}
		}
		return null;
	}


}